package com.yuanfeng.base.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.entity.BaseSearchWordEntity;
import com.yuanfeng.base.mapper.BaseSearchWordMapper;
import com.yuanfeng.base.service.BaseSearchWordService;
import com.yuanfeng.commoms.dto.goods.GoodsMerchantSearchWordDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseMapUtils;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 搜索热门词(BaseSearchWord)
 *
 * @since 2023-03-28 14:15:03
 */
@Service
public class BaseSearchWordServiceImpl extends ServiceImpl<BaseSearchWordMapper, BaseSearchWordEntity>
        implements BaseSearchWordService {

    @Autowired
    private BaseSearchWordMapper baseSearchWordMapper;

    @Override
    public GoodsMerchantSearchWordDTO selectBySearchName(String commonName) {
        return baseSearchWordMapper.selectBySearchName(commonName);
    }

    @Override
    public Integer updateByPrimaryKeySelective(GoodsMerchantSearchWordDTO record) {
        return baseSearchWordMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Integer insertSelective(GoodsMerchantSearchWordDTO record) {
        return baseSearchWordMapper.insertSelective(record);
    }

    @Override
    public ResponseResult querySearchWord(GoodsMerchantSearchWordDTO baseSearchWordDtoReq) {
        //校验token
        String token = baseSearchWordDtoReq.getToken();
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByTokenNoIsExist(token)==null? ParseTokenUtils.getPlatformUser(token):ParseTokenUtils.getUserInfoByToken(token);
        try {
            if (null != userInfo) {
                //将对象转换为map
                Map data = ParseMapUtils.beanToMap(baseSearchWordDtoReq);
                //分页查询数据
                IPage<BaseSearchWordEntity> page = this.baseMapper.querySearchWord(new Query<BaseSearchWordEntity>().getPage(data), baseSearchWordDtoReq);

                return ResponseResult.success(PageUtils.getPage(page));
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);

    }

    @Override
    public ResponseResult queryKeyWords() {
        List<String> keyWords = this.baseSearchWordMapper.queryKeyWords();
        return ResponseResult.success( keyWords);
    }
}

